/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package cn.net.greenet.listener;

import cn.net.greenet.common.db_service.BaseDao;
import javax.servlet.http.HttpSession;
import javax.servlet.http.HttpSessionEvent;
import javax.servlet.http.HttpSessionListener;

/**
 * session 监听器
 *
 * @author liangdelin
 */
public class SessionListener implements HttpSessionListener {

    private HttpSession session = null;

    @Override
    public void sessionCreated(HttpSessionEvent se) {

    }

    /**
     * 当session过期时候 ，改变用户状态 isOn = 0 不在线
     *
     * @param se
     */
    @Override
    public void sessionDestroyed(HttpSessionEvent se) {
        this.session = se.getSession();
        String username = (String) session.getAttribute("name");
        System.out.println("目前正在处理session销毁\t" + username);
        BaseDao baseDao = new BaseDao();
        int row = baseDao.executeUpdate("update user set `isOn` = ?,`last_out` = ? where `name` = ?", new Object[]{0, System.currentTimeMillis(), username});

    }

}
